/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.usfirst.frc2084.robotsimulator.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

/**
 * A panel that contains options for the cRIO: what type it is, 
 * @author Sam
 */
public class cRIOPanel extends JPanel {

    private boolean[] slotsOccupied = {false, false, false, false, false, false, false};
    private boolean crio1 = false;
    private String slot4Module = "Empty";

    /**
     * @param slot
     * @return
     */
    public boolean isSlotOccupied(int slot) {
        return slotsOccupied[slot - 1];
    }

    public String getSlot4Module() {
        return slot4Module;
    }

    private void saveToFile() {
        // TODO: need a better save location
        File save = new File("C:\\Users\\Sam\\Desktop\\cRIO preferences.txt");
        PrintWriter writer;
        try {
            writer = new PrintWriter(save);
            writer.println("cRIO=" + (crio1 ? "I" : "II"));
            for (int i = 1; i <= slotsOccupied.length; i++) {
                writer.println("Slot " + i + "=" + slotsOccupied[i-1]);
                if (i == 4) {
                    writer.println("Slot 4 module=" + slot4Module);
                }
            }
            writer.close();
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(), 
                                          "Saved settings to " + save);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

//                                             //
// Everything below this is generated GUI code //
//                                             //
    /**
     * Creates new form cRIOPanel
     */
    public cRIOPanel() {
        initComponents();
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (Exception ex) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        typeGroup = new ButtonGroup();
        slot1 = new JRadioButton();
        slot2 = new JRadioButton();
        slot3 = new JRadioButton();
        cRIO1 = new JRadioButton();
        cRIO2 = new JRadioButton();
        typeLabel = new JLabel();
        slot5 = new JRadioButton();
        slot7 = new JRadioButton();
        slot6 = new JRadioButton();
        slot4 = new JComboBox();
        slot4Label = new JLabel();
        slotLabel = new JLabel();
        savecRIOButton = new JButton();

        setBorder(BorderFactory.createTitledBorder(null, "cRIO Setup Options", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP, null, Color.black));
        setAutoscrolls(true);
        setMaximumSize(new Dimension(400, 300));
        setMinimumSize(new Dimension(400, 300));
        setName(""); // NOI18N
        setPreferredSize(new Dimension(400, 300));

        slot1.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot1.setText("Analog module in slot 1");
        slot1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot1ActionPerformed(evt);
            }
        });

        slot2.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot2.setText("Digital module in slot 2");
        slot2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot2ActionPerformed(evt);
            }
        });

        slot3.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot3.setText("Solenoid module in slot 3");
        slot3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot3ActionPerformed(evt);
            }
        });

        typeGroup.add(cRIO1);
        cRIO1.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        cRIO1.setSelected(true);
        cRIO1.setText("cRIO I");
        cRIO1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cRIO1ActionPerformed(evt);
            }
        });

        typeGroup.add(cRIO2);
        cRIO2.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        cRIO2.setText("cRIO II");
        cRIO2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                cRIO2ActionPerformed(evt);
            }
        });

        typeLabel.setFont(new Font("Trebuchet MS", 1, 14)); // NOI18N
        typeLabel.setText("cRIO Type");

        slot5.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot5.setEnabled(cRIO1.isSelected());
        slot5.setLabel("Analog module in slot 5");
        slot5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot5ActionPerformed(evt);
            }
        });

        slot7.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot7.setText("Solenoid module in slot 7");
        slot7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot7ActionPerformed(evt);
            }
        });

        slot6.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        slot6.setText("Digital module in slot 6");
        slot6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot6ActionPerformed(evt);
            }
        });

        slot4.setFont(new Font("Trebuchet MS", 0, 11)); // NOI18N
        slot4.setModel(new DefaultComboBoxModel(new String[] { "Empty", "Analog module", "Digital module", "Solenoid module"}));
        slot4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                slot4ActionPerformed(evt);
            }
        });
        slot4.setVisible(false);

        slot4Label.setFont(new Font("Trebuchet MS", 0, 11)); // NOI18N
        slot4Label.setText("Slot 4:");
        slot4Label.setVisible(false);

        slotLabel.setFont(new Font("Trebuchet MS", 1, 14)); // NOI18N
        slotLabel.setText("cRIO Slots");

        savecRIOButton.setFont(new Font("Trebuchet MS", 0, 14)); // NOI18N
        savecRIOButton.setText("Save cRIO settings");
        savecRIOButton.setActionCommand("savecRIO");
        savecRIOButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                savecRIOButtonActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(typeLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                    .addComponent(cRIO2)
                                    .addComponent(cRIO1)))
                            .addComponent(slotLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(savecRIOButton)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(slot4Label, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(slot4, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(slot1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slot2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slot3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slot5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slot6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slot7, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap(164, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(typeLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRIO1)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cRIO2)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slotLabel)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot1)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot2)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot3)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(slot4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(slot4Label))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot5)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot6)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slot7)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(savecRIOButton)
                .addContainerGap())
        );
    }// </editor-fold>

	private void slot1ActionPerformed(ActionEvent evt) {
        slotsOccupied[0] = !slotsOccupied[0];
    }

	private void slot2ActionPerformed(ActionEvent evt) {
        slotsOccupied[1] = !slotsOccupied[1];
    }

	private void slot3ActionPerformed(ActionEvent evt) {
        slotsOccupied[2] = !slotsOccupied[2];
    }

    private void slot4ActionPerformed(ActionEvent evt) {
        slotsOccupied[3] = slot4.getSelectedIndex() != 0;
        slot4Module = slot4.getSelectedItem().toString();
    }

    private void slot5ActionPerformed(ActionEvent evt) {
        slotsOccupied[4] = !slotsOccupied[4];
    }

	private void slot6ActionPerformed(ActionEvent evt) {
        slotsOccupied[5] = !slotsOccupied[5];
    }

	private void slot7ActionPerformed(ActionEvent evt) {
        slotsOccupied[6] = !slotsOccupied[6];
    }

	private void cRIO2ActionPerformed(ActionEvent evt) {
        cRIO1ActionPerformed(evt);
    }
	
    private void cRIO1ActionPerformed(ActionEvent evt) {
        slot4Label.setVisible(!cRIO1.isSelected());
        slot4.setVisible(!cRIO1.isSelected());
        slot5.setVisible(cRIO1.isSelected());
        slot6.setVisible(cRIO1.isSelected());
        slot7.setVisible(cRIO1.isSelected());
        crio1 = cRIO1.isSelected();
    }

    private void savecRIOButtonActionPerformed(ActionEvent evt) {
        if ((!slot1.isSelected() && !slot2.isSelected() && !slot3.isSelected() && slot4.getSelectedIndex() == 0 && cRIO2.isSelected())
                || ((!slot1.isSelected() && !slot2.isSelected() && !slot3.isSelected() && !slot5.isSelected() && !slot6.isSelected() && !slot7.isSelected() && cRIO1.isSelected()))) {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(), 
                                          "You must have at least one module in the cRIO!", 
                                          null, 
                                          JOptionPane.ERROR_MESSAGE);
        } else {
            saveToFile();
        }
    }
    // Variables declaration - do not modify
    private ButtonGroup typeGroup;
    private JRadioButton cRIO1;
    private JRadioButton cRIO2;
    private JButton savecRIOButton;
    private JRadioButton slot1;
    private JRadioButton slot2;
    private JRadioButton slot3;
    private JComboBox slot4;
    private JLabel slot4Label;
    private JRadioButton slot5;
    private JRadioButton slot6;
    private JRadioButton slot7;
    private JLabel slotLabel;
    private JLabel typeLabel;
   // End of variables declaration
}







//CodeCleaner -- do not edit this line
//CodeCleaner -- do not edit this line
//CodeCleaner -- do not edit this line
//CodeCleaner -- do not edit this line
//CodeCleaner -- do not edit this line
//CodeCleaner -- do not edit this line

